/**
 * 
 */

package de.unihannover.se.restlab.microblog.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.sun.jersey.api.NotFoundException;

import de.unihannover.se.restlab.microblog.data.MessageStorage;
import de.unihannover.se.restlab.microblog.data.UserStorage;
import de.unihannover.se.restlab.microblog.models.Message;
import de.unihannover.se.restlab.microblog.models.User;

public class MessageResource {

	private Message message;

	public MessageResource (Integer id) {
		this.message = MessageStorage.getStorage().getMessage(id);
		if (message == null) throw new NotFoundException();
	}

	@GET
	@Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public Response show () {
		return Response.ok(message).build();
	}

	/* @GET
	 * 
	 * @Path("test") public Response helloWorld() { return Response.ok("HelloWorld").build(); } */

	@POST
	@Path("reblog")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public Response createRebloggedMessage (@FormParam("userId") Integer userId) {
		System.out.println("HERE");
		User user = UserStorage.getStorage().getUser(userId);
		Message msg = MessageStorage.getStorage().createMessage(user, message.getContent(), message.getId());
		return Response.ok(msg).build();
	}
}
